package dev.pda.display.presentation.page.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import dev.pda.display.Constants
import dev.pda.display.presentation.page.model.PageData
import dev.pda.display.presentation.page.model.WidgetData
import dev.pda.display.presentation.page.model.WidgetModel
import dev.pda.display.presentation.page.model.widget.ImageWidget
import dev.pda.display.presentation.page.model.widget.TextWidget
import dev.pda.display.presentation.page.model.widget.UrlWidget
import dev.pda.display.presentation.page.model.widget.VideoWidget
import dev.pda.display.util.queue.QueueController
import java.io.File


class PageView : RelativeLayout {

    private var widgetData: WidgetData<PageData<WidgetModel<Any>>>? = null

    constructor(context: Context?) : super(context) {
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    }

    @SuppressLint("ResourceType")
    fun initViews(widgetData: WidgetData<PageData<WidgetModel<Any>>>?) {
        this.widgetData = widgetData
        widgetData?.let { data ->
            val pageVM = data.items?.get(0)
            val bgImageView = ImageView(context)
            bgImageView.apply {
                id = 10000
                layoutParams = ViewGroup.LayoutParams(
                    getProgramWidth(),
                    getProgramHeight()
                )
                scaleType = ImageView.ScaleType.FIT_XY
            }
            val bgImageUrl = "http" + pageVM?.bg
            Glide.with(context).load(bgImageUrl).into(bgImageView)
            this.addView(bgImageView)
            pageVM?.widgets?.forEachIndexed { index, widget ->
                when (widget) {
                    is ImageWidget -> {
                        val imageView = CustomImageView(context)
                        imageView.apply {
                            setImageViewMethod(widget)
                            id = index
                            layoutParams = LinearLayout.LayoutParams(
                                getViewWidth(widget.width),
                                getViewHeight(widget.height)
                            ).apply {
                                setMargins(getViewX(widget.x), getViewHeight(widget.y), 0, 0)
                            }
                        }
                        this.addView(imageView)
                    }
                    is TextWidget -> {
                        val textView = CustomTextView(context)
                        textView.apply {
                            id = index
                            setTextMethod(widget)
                            layoutParams = LinearLayout.LayoutParams(
                                getViewWidth(widget.width),
                                getViewHeight(widget.height)
                            ).apply {
                                setMargins(getViewX(widget.x), getViewHeight(widget.y), 0, 0)
                            }
                        }
                        this.addView(textView)
                    }
                    is VideoWidget -> {
                        val videoView = VideoView(context)
                        val testPath = File(
                            QueueController().getParentFile(context),
                            Constants.FILE_DOWNLOAD_PATH + "/" + widget.item?.fileName
                        ).path
                        videoView.apply {
                            id = index
                            initVideoView(testPath)
                            layoutParams = LinearLayout.LayoutParams(
                                getViewWidth(widget.width),
                                getViewHeight(widget.height)
                            ).apply {
                                setMargins(getViewX(widget.x), getViewHeight(widget.y), 0, 0)
                            }
                        }
                        this.addView(videoView)
                    }
                    is UrlWidget -> {
                        val urlView = UrlView(context)
                        urlView.apply {
                            id = index
                            widget.item?.content?.let { loadUrl(it) }
                            layoutParams = LinearLayout.LayoutParams(
                                getViewWidth(widget.width),
                                getViewHeight(widget.height)
                            ).apply {
                                setMargins(getViewX(widget.x), getViewHeight(widget.y), 0, 0)
                            }
                        }
                        this.addView(urlView)
                    }
                }
            }

        }
    }

    private fun getProgramWidth(): Int {
        return widgetData?.programWidth?.toInt() ?: 0
    }

    private fun getProgramHeight(): Int {
        return widgetData?.programHeight?.toInt() ?: 0
    }

    private fun getViewWidth(viewWidth: Double?): Int {
        return (viewWidth?.let { getProgramWidth().times(it) })?.toInt() ?: 0
    }

    private fun getViewHeight(viewHeight: Double?): Int {
        return (viewHeight?.let { getProgramHeight().times(it) })?.toInt() ?: 0
    }

    private fun getViewX(viewWidth: Double?): Int {
        return (viewWidth?.let { getProgramWidth().times(it) })?.toInt() ?: 0
    }

    private fun getViewY(viewHeight: Double?): Int {
        return (viewHeight?.let { getProgramHeight().times(it) })?.toInt() ?: 0
    }
}